<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container" >
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" >
        <el-form-item label="任务名称" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请输入任务名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="创建时间">
          <el-date-picker
            v-model="queryParams.createTime"
            :editable="false"
            type="daterange"
            range-separator="~"
            start-placeholder="开始时间"
            end-placeholder="截止时间"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item class="search-buttons">
          <el-button class="filter-item" type="primary" icon="search" @click="handleQuery">
            搜索
          </el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  
    <el-card shadow="hover" class="data-table">
      <div class="data-table__toolbar">
        <div class="data-table__toolbar--actions">
          <el-button
            type="success"
            icon="plus"
            @click="handleAuditDialog()"
          >
            全部通过
          </el-button>
          
        </div>
      </div>

      <el-table
        v-loading="loading"
        :data="Lists"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        class="data-table__content"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="id" label="序号"  />
        <el-table-column prop="account" label="账号"  />


        <el-table-column prop="avatar" label="头像" >
          <template #default="{ row }">
            <el-image
              style="width: 50px; height: 50px; border-radius: 50%"
              :src="row.avatar || 'https://xcx.yumeishangmao.com/uploads/8b112625e64da7e7dc69fab1044487e5.png'"
              :preview-src-list="[row.avatar]"
            />
          </template>
        </el-table-column>

        <el-table-column prop="task_name" label="任务名称" />

        <el-table-column prop="avatar" label="任务图片" >
          <template #default="{ row }">
            <el-image
              style="width: 50px; height: 50px; border-radius: 50%"
              :src="row.task_logo || 'https://xcx.yumeishangmao.com/uploads/8b112625e64da7e7dc69fab1044487e5.png'"
              :preview-src-list="[row.task_logo]"
            />
          </template>
        </el-table-column>

        <el-table-column prop="createTime" label="创建时间"  />


        <!-- <el-table-column prop="sort" label="排序" width="150" /> -->
         <el-table-column prop="status" label="状态" >
          <template #default="scope">
            <el-tag v-if="scope.row.status == 1" type="success">已通过</el-tag>
            <el-tag v-else-if="scope.row.status == 2" type="warning">已驳回</el-tag>
            <el-tag v-else type="info">未审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="auditTime" label="审核时间"/>

        <el-table-column label="操作" fixed="right" align="left" width="200">
          <template #default="scope">
            
            <el-button
              v-hasPerm="['task:task:edit']"
              type="primary"
              link
              size="small"
              icon="edit"
              @click.stop="handleOpenDialog(scope.row.id,scope.row,1)"
            >
              通过
            </el-button>

            <el-button
              v-hasPerm="['task:task:edit']"
              type="primary"
              link
              size="small"
              icon="edit"
              @click.stop="handleOpenDialog(scope.row.id,scope.row,2)"
            >
              拒绝
            </el-button>
            
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="600px"
      @closed="handleCloseDialog"
    >
      <el-form ref="FormRef" :model="formData" :rules="rules" label-width="120px">
        
        <el-form-item label="状态" >
          <el-radio-group v-model="formData.status">
            <el-radio v-if="dialog_status==1" :value="1">通过</el-radio>
            <el-radio v-if="dialog_status==2" :value="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "任务审核",
  inheritAttrs: false,
});

import TaskAPI, { TaskListVO, TaskListForm } from "@/api/system/task.api";

const queryFormRef = ref();
const FormRef = ref();

const loading = ref(false);
const selectIds = ref<number[]>([]);
const queryParams = reactive<any>({});

const dialog = reactive({
  title: "",
  visible: false,
});

const Lists = ref<TaskListVO[]>();
const formData = reactive<TaskListForm>({
  auditTime: "",
  id: undefined,
  status: 1,
});

const rules = reactive({
  name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  sort: [{ required: true, message: "显示排序不能为空", trigger: "blur" }],
});

// 查询
function handleQuery() {
  console.log(queryParams)
  loading.value = true;

  Lists.value =  [
    {
      id:"1",
      account: "zhangsan",
      sort:1,status:1,
      createTime:"2023-10-10 10:00:00",
      avatar:"https://xcx.yumeishangmao.com/uploads/8b112625e64da7e7dc69fab1044487e5.png",
      task_logo:"https://xcx.yumeishangmao.com/uploads/8b112625e64da7e7dc69fab1044487e5.png",
      task_name:"一级任务",
      auditTime:"2023-10-10 10:00:00",
    },
    {
      id:"2",
      account: "lisi",
      sort:2,status:2,
      createTime:"2023-10-10 10:00:00",
      avatar:"https://xcx.yumeishangmao.com/uploads/8b112625e64da7e7dc69fab1044487e5.png",
      task_logo:"https://xcx.yumeishangmao.com/uploads/8b112625e64da7e7dc69fab1044487e5.png",
      task_name:"二级任务",
      auditTime:"2023-10-10 10:00:00",
    },
    {
      id:"3",
      account: "wangwu",
      sort:3,status:0,
      createTime:"2023-10-10 10:00:00",
      avatar:"https://xcx.yumeishangmao.com/uploads/8b112625e64da7e7dc69fab1044487e5.png",
      task_logo:"https://xcx.yumeishangmao.com/uploads/8b112625e64da7e7dc69fab1044487e5.png",
      task_name:"三级任务",
      auditTime:"2023-10-10 10:00:00",
    }
  ]
  loading.value = false;
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

// 处理选中项变化
function handleSelectionChange(selection: any) {
  selectIds.value = selection.map((item: any) => item.id);
}

function handleAuditDialog(Id?: number) {
  const Ids = [Id || selectIds.value].join(",");

  if (!Ids) {
    ElMessage.warning("请勾选选项");
    return;
  }

  ElMessageBox.confirm("确认全部通过已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      ElMessage.success("操作成功");
      // TaskAPI.AuditByIds(Ids)
      //   .then(() => {
      //     ElMessage.success("操作成功");
      //     handleResetQuery();
      //   })
      //   .finally(() => (loading.value = false));
      loading.value = false
    },
    () => {
      ElMessage.info("已取消");
    }
  );
}


const dialog_status = ref<number>(1);

/**
 * 打开新增弹窗
 *
 * @param Id ID
 */
async function handleOpenDialog(Id?:string,data?:TaskListVO,status?:number) {

  dialog.visible = true;
  if (Id) {
    dialog.title = "修改";
    dialog_status.value = status || 1;
    if(data&&status){
      data.status = status || 1;
    }
    Object.assign(formData, data);
  } else {
    dialog.title = "新增";
    dialog_status.value = status || 1;
  }
}

// 提交表单
function handleSubmit() {
  FormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      
      ElMessage.success("修改成功");
      // TaskAPI.update(formData.id, formData)
      //   .then(() => {
      //     ElMessage.success("修改成功");
      //     handleCloseDialog();
      //     handleQuery();
      //   })
      //   .finally(() => (loading.value = false));
      
      handleCloseDialog();
      handleQuery();
      loading.value = false
    }
  });
}



// 重置表单
function resetForm() {
  FormRef.value.resetFields();
  FormRef.value.clearValidate();
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  resetForm();
}

onMounted(() => {
  handleQuery();
});
</script>
